﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

/// <summary>
/// 物品模板
/// </summary>
public class ArticleTemplet {

    /// <summary>
    /// 物品模板id
    /// </summary>
	public string ArticleTempletId { get; set; }
    /// <summary>
    /// 物品类型
    /// </summary>
    public Constant.ArticleCategory ArticleCategory { get; set; }
    /// <summary>
    /// 物品模板名
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 物品描述
    /// </summary>
    public string Intro { get; set; }
    /// <summary>
    /// 空间数量
    /// </summary>
    public int SpaceNum { get; set; }
    /// <summary>
    /// 基础购买价格
    /// </summary>
    public int BuyPrice { get; set; }
    /// <summary>
    /// 购买价格波动s
    /// </summary>
    public int BuyPriceRange { get; set; }
    /// <summary>
    /// 基础出售价格
    /// </summary>
    public int SellPrice { get; set; }
    /// <summary>
    /// 出售价格波动
    /// </summary>
    public int SellPriceRange { get; set; }
    /// <summary>
    /// 基础攻击
    /// </summary>
    public int Attack { get; set; }
    /// <summary>
    /// 攻击波动
    /// </summary>
    public int AttackRange { get; set; }
    /// <summary>
    /// 基础防御
    /// </summary>
    public int Defense { get; set; }
    /// <summary>
    /// 防御波动
    /// </summary>
    public int DefenseRange { get; set; }
    /// <summary>
    /// 基础生命
    /// </summary>
    public int Hp { get; set; }
    /// <summary>
    /// 生命波动
    /// </summary>
    public int HpRange { get; set; }
    /// <summary>
    /// 基础内力
    /// </summary>
    public int Mp { get; set; }
    /// <summary>
    /// 内力波动
    /// </summary>
    public int MpRange { get; set; }
    /// <summary>
    /// 基础体力
    /// </summary>
    public int Vit { get; set; }
    /// <summary>
    /// 体力波动
    /// </summary>
    public int VitRange { get; set; }
    /// <summary>
    /// 基础速度
    /// </summary>
    public int Speed { get; set; }
    /// <summary>
    /// 速度波动
    /// </summary>
    public int SpeedRange { get; set; }
    /// <summary>
    /// 图片地址
    /// </summary>
    public string Sprite { get; set; }

    /// <summary>
    /// 需求条件
    /// </summary>
    public List<RequireCondition> RequireConditions { get; set; }

    /// <summary>
    /// 根据物品模板生成一个物品
    /// </summary>
    /// <returns></returns>
    public Article GenerateArticle() {
        System.Random random = GameManager.Instance.Random;
        if (this.ArticleCategory == Constant.ArticleCategory.Drug // 药品
            || this.ArticleCategory == Constant.ArticleCategory.Nutriment // 营养品
            || this.ArticleCategory == Constant.ArticleCategory.QuestItem // 任务物品
            || this.ArticleCategory == Constant.ArticleCategory.WuxueEsoterica) { // 武学秘籍
            Article article = new Article(this);
            article.Name = this.Name;
            article.CategoryName = ObjectHelper.GetDescription(this.ArticleCategory);
            article.BuyPrice = this.BuyPrice;
            article.SellPrice = this.SellPrice;
            article.Attack = this.Attack;
            article.Defense = this.Defense;
            article.Hp = this.Hp;
            article.Mp = this.Mp;
            article.Vit = this.Vit;
            article.Speed = this.Speed;
            this.FillIntro(article);
            return article;
        } else {
            int chance = random.Next(100);
            int[] boundary = { 10, 55, 79, 91, 97 }; // 品质分割
            Constant.ArticleQuality quality;
            if (chance < boundary[0]) {
                quality = Constant.ArticleQuality.Damaged;
            } else if (chance >= boundary[0] && chance < boundary[1]) {
                quality = Constant.ArticleQuality.Ordinary;
            } else if (chance >= boundary[1] && chance < boundary[2]) {
                quality = Constant.ArticleQuality.Exquisite;
            } else if (chance >= boundary[2] && chance < boundary[3]) {
                quality = Constant.ArticleQuality.Singular;
            } else if (chance >= boundary[3] && chance < boundary[4]) {
                quality = Constant.ArticleQuality.Epic;
            } else {
                quality = Constant.ArticleQuality.Legendary;
            }
            string description = ObjectHelper.GetDescription(quality);
            Article article = new Article(this, quality);
            article.Name = string.Format("<color={0}>{1}{2}</color>", Constant.ARTICLE_QUALITY_COLOR[(int)quality], description, this.Name);
            article.CategoryName = ObjectHelper.GetDescription(this.ArticleCategory);

            /**
             * 有波动的值
             **/
            article.BuyPrice = getQualityPropVal(random, quality, this.BuyPrice, this.BuyPriceRange);
            article.SellPrice = getQualityPropVal(random, quality, this.SellPrice, this.SellPriceRange);
            article.Attack = getQualityPropVal(random, quality, this.Attack, this.AttackRange);
            article.Defense = getQualityPropVal(random, quality, this.Defense, this.DefenseRange);
            article.Hp = getQualityPropVal(random, quality, this.Hp, this.HpRange);
            article.Mp = getQualityPropVal(random, quality, this.Mp, this.MpRange);
            article.Vit = getQualityPropVal(random, quality, this.Vit, this.VitRange);
            article.Speed = getQualityPropVal(random, quality, this.Speed, this.SpeedRange);

            this.FillIntro(article);

            return article;
        }
    }

    /// <summary>
    /// 根据品质、基本值与波动值计算实际值
    /// </summary>
    /// <param name="articleQuality">品质</param>
    /// <param name="baseVal">基本值</param>
    /// <param name="range">波动值</param>
    /// <returns></returns>
    private static int getQualityPropVal(System.Random r, Constant.ArticleQuality articleQuality, int baseVal, int range) {
        int val = baseVal;
        int quality = (int)articleQuality;
        if (range != 0 && articleQuality != Constant.ArticleQuality.Ordinary) {
            int rangeVal;
            if (range > 0) {
                rangeVal = r.Next(range);
            } else {
                rangeVal = -r.Next(-range);
            }
            if (quality > 1) { // 增益属性
                val += range * (quality - 2) + rangeVal;
            } else { // 减损属性
                val -= rangeVal;
            }
        }
        return val;
    }

    /// <summary>
    /// 填充物品简介
    /// </summary>
    /// <param name="article"></param>
    private void FillIntro(Article article) {
        StringBuilder sb = new StringBuilder(this.Intro);
        sb.Append(ConstructPropMessage("\n攻击力", article.Attack));
        sb.Append(ConstructPropMessage("\n防御力", article.Defense));
        sb.Append(ConstructPropMessage("\n生命", article.Hp));
        sb.Append(ConstructPropMessage("\n内力", article.Mp));
        sb.Append(ConstructPropMessage("\n体力", article.Vit));
        sb.Append(ConstructPropMessage("\n速度", article.Speed));
        if (this.RequireConditions.Count > 0) {
            sb.Append("\n<color=" + Constant.ArticleMeetConditionsColor + ">").Append(this.ArticleCategory == Constant.ArticleCategory.WuxueEsoterica ? "学习条件：" : "装备条件：").Append("</color>");
            foreach (RequireCondition requireCondition in this.RequireConditions) {
                string prop = ObjectHelper.GetDescription(requireCondition.Prop);
                string compare = ObjectHelper.GetDescription(requireCondition.Compare);
                sb.Append("\n<color={" + requireCondition.Prop + "}>").Append(prop + " " + compare + " " + requireCondition.Val).Append("</color>");
            }
        }
        sb.Append("\n<color=" + Constant.ArticlePriceColor + ">购买价格：").Append(article.BuyPrice).Append(" 出售价格：").Append(article.SellPrice).Append("</color>");
        article.Intro = sb.ToString();
    }

    /// <summary>
    /// 构造属性信息
    /// </summary>
    /// <param name="propName"></param>
    /// <param name="propValue"></param>
    /// <returns></returns>
    private string ConstructPropMessage(string propName, int propValue) {
        if (propValue == 0) {
            return "";
        }
        string color;
        string symbol;
        int value;
        if (propValue > 0) { // 正数
            color = Constant.ArticlePlusPropColor;
            symbol = "+";
            value = propValue;
        } else { // 负数
            color = Constant.ArticleMinusPropColor;
            symbol = "-";
            value = -propValue;
        }
        return "<color=" + color + ">" + propName + " " + symbol + " " + value + "</color>";
    }
}
